<?php
class catalog_tree
{
	public $registry_parent_id = array();
	public $mode = "catalog_tree";
	
	public function __construct($table) {
		$this->table = $table;
	}

	public function getMenu($id, $i, $catalog_parent)
	{		
		$x = 10 * $i;	
		
		$i++;
		$r =& Registry::get("MDB")->query("SELECT * FROM `".$this->table."` WHERE `Parent`='".$id."' ORDER BY `Por`;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if($r->numRows())
		{
			while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) 
			{				
				$this->menuArrayLink[$p['id']] = array(
					"count"    => $x,
					"iterator" => $i,
					"id"       => $p['id'],
					"parent"   => $p['parent'],
					"m_param_id" => ($this->m_param_id ? $this->m_param_id : ''),
					"m_param_parent" => ($this->m_param_parent ? $this->m_param_parent : ''),
					"name"     => $p['name'],
					"table"    => $this->table,
					"mode"     => "tree".$this->table,
					"is_plus"  => (functions::isChildrens($this->table, $p['id']) ? (in_array($p['id'], $this->registry_parent_id) ? "opened" : "to_open") : "none"),
					"is_color" => (in_array($p['id'], $this->registry_parent_id) ? 1 : 0)
				);

				if(($id != $catalog_parent) && in_array($p['id'], $this->registry_parent_id)) {
					self::getMenu($p['id'], $i, $catalog_parent);
				}
			}
			return $this->menuArrayLink;
		}
	}

	/* Дерево открытых директорий */
	public function registry_parent_id($id) {		
		$r =& Registry::get("MDB")->query("SELECT `ID`,`Parent` FROM `".$this->table."` WHERE `ID`='".$id."' LIMIT 1;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if($r->numRows()) {
			while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$this->registry_parent_id[] = $p['id'];
				if($p['parent']) {
					self::registry_parent_id($p['parent']);
				}
			}
		}
	}
}

?>
